<?php

namespace App\V3\V3_2_7\Services;

use App\V3\V3_2_7\Models\ApplicationFormModel;
use App\V3\V3_2_7\Models\ApplicationGoodsModel;
use App\V3\V3_2_7\Models\MaterielProductContentModel;
use App\V3\V3_2_7\Models\StorageManageModel;

class ApplicationGoodsService
{
    /**
     *申请单商品入库详情
     * Author: zlf
     * Created at 2024/6/6 9:48
     * @param $data
     * @return void
     */
    public static function inApplicationGoodsInfo($data)
    {
        $applicationId = ApplicationFormModel::query()
            ->where([
                'id' => $data['id'],
                'delete_flag' => 0,
                'repeal_status' => 0,
                'main_id' => $data['main_id']
            ])->value('id');
        if (!empty($applicationId)) {
            $goodslist = '';
            if (ApplicationGoodsModel::query()
                ->where('application_id', $applicationId)->exists()) {
                $goodslist = ApplicationGoodsModel::query()
                    ->select(
                        'b.id',
                        'b.id as type_id',
                        'b.materiel_name',
                        'a.type_name',
                        'b.materiel_img',
                        'materiel_no',
                        'cost',
                        'd.id as f_class_id',
                        'd.class_name as f_class_name',
                        'd.class_no as f_class_no',
                        'c.id as s_class_id',
                        'c.class_name as s_class_name',
                        'c.class_no as s_class_no',
                        'b.is_product_materiel',
                        'b.selling_price',
                        'b.lower_selling_price',
                        'b.remark',
                        'b.is_use_serial',
                        'b.materiel_unit',
                        'b.is_product_materiel',
                        'a.num'
                    )->from('application_goods as a')
                    ->leftJoin('materiel_type as b', 'b.id', '=', 'a.type_id')
                    ->leftJoin('materiel_class as c', 'b.materiel_class_id', '=', 'c.id')
                    ->leftJoin('materiel_class as d', 'd.id', '=', 'c.parent_id')
                    ->where([
                        'b.delete_flag' => 0,
                        'a.main_id' => $data['main_id'],
                        'application_id' => $applicationId
                    ])->get();
            }
            $list = '';
            if (!empty($goodslist->toArray())) {
                $list = $goodslist->toArray();
                foreach ($list as &$value) {
                    if (!empty($value['materiel_img'])) {
                        if (strpos($value['materiel_img'], ',') !== false) {
                            $materielImg = explode(',', $value['materiel_img']);
                            if (empty($materielImg[0])) $materielImg[0] = $materielImg[1];
                            $value['materiel_img'] = $materielImg[0];
                        }
                    }
                    $value['content_type_id'] = [];
                    if ($value['is_product_materiel']) {
                        $value['content_type_id'] = MaterielProductContentModel::where([
                            'product_id' => $value['id']
                        ])->pluck('type_id')->toArray();
                    }
                }
            }
            return ['code' => 0, 'msg' => 'success', 'data' => $list];
        } else {
            return ['code' => 1, 'msg' => '无申请单商品信息！', 'data' => ''];
        }
    }

    /**
     *申请单商品出库详情
     * Author: zlf
     * Created at 2024/6/6 9:48
     * @param $data
     * @return void
     */
    public static function outApplicationGoodsInfo($data)
    {
        $applicationId = ApplicationFormModel::query()
            ->where([
                'id' => $data['id'],
                'delete_flag' => 0,
                'repeal_status' => 0,
                'main_id' => $data['main_id']
            ])->value('id');
        if (!empty($applicationId)) {
            $goodslist = '';
            if (ApplicationGoodsModel::query()
                ->where('application_id', $applicationId)->exists()) {
                $goodslist = ApplicationGoodsModel::query()
                    ->select(
                        'b.materiel_name',
                        'b.id as type_id',
                        'b.type_name',
                        'b.materiel_no',
                        'b.materiel_img',
                        'd.id as f_class_id',
                        'd.class_name as f_class_name',
                        'd.class_no as f_class_no',
                        'c.id as s_class_id',
                        'c.class_name as s_class_name',
                        'c.class_no as s_class_no',
                        'b.is_product_materiel',
                        'b.selling_price',
                        'b.lower_selling_price',
                        'b.remark',
                        'b.is_use_serial',
                        'b.bar_code',
                        'b.materiel_unit',
                        'b.is_product_materiel',
                        'a.id as application_goods_id',
                        'surplus_num as num'
                    )->from('application_goods as a')
                    ->leftJoin('materiel_type as b', 'b.id', '=', 'a.type_id')
                    ->leftJoin('materiel_class as c', 'b.materiel_class_id', '=', 'c.id')
                    ->leftJoin('materiel_class as d', 'd.id', '=', 'c.parent_id')
                    ->where([
                        'b.delete_flag' => 0,
                        ['surplus_num', '>', 0],
                        'a.main_id' => $data['main_id'],
                        'application_id' => $applicationId,
                    ])->get();
            }
            $resData = [];
            if (!empty($goodslist->toArray())) {
                $list = $goodslist->toArray();
                foreach ($list as &$value) {
                    if (!empty($value['materiel_img'])) {
                        if (strpos($value['materiel_img'], ',') !== false) {
                            $materielImg = explode(',', $value['materiel_img']);
                            if (empty($materielImg[0])) $materielImg[0] = $materielImg[1];
                            $value['materiel_img'] = $materielImg[0];
                        }
                    }
                    $manageInfo = StorageManageModel::query()
                        ->select(
                            'a.id',
                            'a.materiel_mean_cost as cost',
                            'a.storage_id',
                            'b.storage_name',
                            'b.storage_address',
                            'a.in_storage_num',
                            'a.alert_num',
                            'a.virtual_storage_num',
                            'a.shelf_id',
                            'a.location_id',
                            'c.shelf_name',
                            'd.location_name',
                            'a.sort_flag',
                            'time_warning_flag'
                        )->from('storage_manage as a')
                        ->leftJoin('storage as b', 'b.id', '=', 'a.storage_id')
                        ->leftJoin('storage_shelf as c', 'c.id', '=', 'a.shelf_id')
                        ->leftJoin('storage_shelf_location as d', 'd.id', '=', 'a.location_id')
                        ->where([
                            'type_id' => $value['type_id'],
                            'a.main_id' => $data['main_id'],
                            'a.delete_flag' => 0,
                            'b.delete_flag' => 0,
                            'c.status' => 1,
                            'd.status' => 1,
                        ])->orderBy('in_storage_num', 'desc')->first();

                    if (!empty($manageInfo)) {
                        $manageInfo = $manageInfo->toArray();
                    } else {
                        $manageInfo = [
                            'id' => '',
                            'cost' => '',
                            'storage_id' => '',
                            'storage_name' => '',
                            'storage_address' => '',
                            'in_storage_num' => '',
                            'alert_num' => '',
                            'virtual_storage_num' => '',
                            'shelf_id' => '',
                            'location_id' => '',
                            'shelf_name' => '',
                            'location_name' => '',
                            'sort_flag' => '',
                            'time_warning_flag' => ''
                        ];
                    }
                    $resData[] = array_merge($value, $manageInfo);
                }
            }
            return ['code' => 0, 'msg' => 'success', 'data' => $resData];
        } else {
            return ['code' => 1, 'msg' => '无出库的商品信息！', 'data' => ''];
        }
    }
}
